package config

import (
	"production-tracker/models"
	"gorm.io/gorm"
)

// MigrateDB 自动迁移数据库表
func MigrateDB(db *gorm.DB) {
	db.AutoMigrate(
		&models.WorkPlan{},
		&models.Material{},
		&models.WorkOrderData{},
		&models.Stencil{},
		&models.Fixture{},
		&models.SOPApproval{},
	)
}

// InitSampleData 初始化示例数据
func InitSampleData(db *gorm.DB) {
	// 检查是否已有数据
	var count int64
	db.Model(&models.WorkPlan{}).Count(&count)
	if count > 0 {
		return
	}

	// 初始化工作进度计划数据
	workPlans := []models.WorkPlan{
		{Model: gorm.Model{ID: 1}, ModelName: "Model-A", WorkOrder: "WO-2024-001", Quantity: 100, Date: "2024-01-15", Status: "pending", Notes: "紧急订单"},
		{Model: gorm.Model{ID: 2}, ModelName: "Model-B", WorkOrder: "WO-2024-002", Quantity: 200, Date: "2024-01-20", Status: "in-progress", Notes: "正常生产"},
		{Model: gorm.Model{ID: 3}, ModelName: "Model-C", WorkOrder: "WO-2024-003", Quantity: 150, Date: "2024-01-10", Status: "delayed", Notes: "物料延迟"},
	}

	// 初始化物料数据
	materials := []models.Material{
		{Model: gorm.Model{ID: 1}, Serial: 1, ModelName: "Model-A", Name: "电阻", Quantity: 1000, Location: "仓库A", Notes: "标准件"},
		{Model: gorm.Model{ID: 2}, Serial: 2, ModelName: "Model-B", Name: "电容", Quantity: 2000, Location: "仓库B", Notes: "高容量"},
	}

	// 初始化数据查询数据
	workOrderData := []models.WorkOrderData{
		{Model: gorm.Model{ID: 1}, WorkOrder: "WO-2024-001", WorkOrderType: "生产工单", PartNumber: "PN-001", PartName: "主板", Specification: "标准", Category: "电子", WorkOrderQuantity: 100, Version: "V1.0", ProductionDate: "2024-01-15", Process: "SMT", StencilTop: true, StencilBot: false, ReflowFixture: true, WaveFixture: false, PressFixture: true, BOM: true, SOP: true, ProductionMaterials: true, Type: "标准"},
	}

	// 初始化钢网数据
	stencils := []models.Stencil{
		{Model: gorm.Model{ID: 1}, Serial: 1, ModelName: "Model-A", Quantity: 100, OpenTime: "2024-01-05", AcceptanceTime: "2024-01-10", Status: "completed", Notes: "已验收"},
		{Model: gorm.Model{ID: 2}, Serial: 2, ModelName: "Model-B", Quantity: 200, OpenTime: "2024-01-08", AcceptanceTime: "2024-01-12", Status: "in-progress", Notes: "验收中"},
	}

	// 初始化治具数据
	fixtures := []models.Fixture{
		{Model: gorm.Model{ID: 1}, Serial: 1, ModelName: "Model-A", Quantity: 50, OpenTime: "2024-01-06", AcceptanceTime: "2024-01-11", Status: "completed", Notes: "已入库"},
		{Model: gorm.Model{ID: 2}, Serial: 2, ModelName: "Model-B", Quantity: 100, OpenTime: "2024-01-09", AcceptanceTime: "2024-01-13", Status: "pending", Notes: "待验收"},
	}

	// 初始化SOP数据
	sopApprovals := []models.SOPApproval{
		{Model: gorm.Model{ID: 1}, Serial: 1, ModelName: "Model-A", Status: "approved", SubmitTime: "2024-01-05", ApprovalTime: "2024-01-08", Notes: "已通过"},
		{Model: gorm.Model{ID: 2}, Serial: 2, ModelName: "Model-B", Status: "pending", SubmitTime: "2024-01-07", Notes: "待签核"},
	}

	// 批量插入数据
	db.CreateInBatches(workPlans, len(workPlans))
	db.CreateInBatches(materials, len(materials))
	db.CreateInBatches(workOrderData, len(workOrderData))
	db.CreateInBatches(stencils, len(stencils))
	db.CreateInBatches(fixtures, len(fixtures))
	db.CreateInBatches(sopApprovals, len(sopApprovals))
}
